﻿// Example header text. Can be configured in the options.
using vtech_formBase;
using System;
using vtech_frameWorks.Exceptions;
using System.Data;

namespace PharmacySystem.BusinessObject.Department
{
    public partial class frmEditDepartment : frmEdit
    {
        public delegate void EditDelegate();
        public event EditDelegate OnEdit = null;

        protected override void FireEvent()
        {
            if (this.OnEdit != null)
            {
                this.OnEdit();
            }
        }

        #region Contructror
        
        #endregion

        public frmEditDepartment()
        {
            this.InitializeComponent();
        }

        public frmEditDepartment(Guid ID)
        {
            this.InitializeComponent();
            this.ID = ID;
        }

        protected override void GetData()
        {
            ClsDepartmentDAO obj = new ClsDepartmentDAO();
            DataRow[] drow;
            drow = obj.GetAll().Select(string.Format("ID='{0}'", this.ID));
            foreach (DataRow row in drow)
            {
                this.txtDepartmentCode.Text = row["Code"].ToString();
                this.txtDepartmentName.Text = row["Name"].ToString();
                this.txtDescription.Text = row["Description"].ToString();
            }
        }

        protected override bool CheckControl()
        {
            bool flag = false;
            try
            {
                this.errorProvider.Dispose();
                if (this.txtDepartmentCode.Text == "")
                {
                    this.errorProvider.SetError(this.txtDepartmentCode, "Bạn chưa nhập mã. Xin vui lòng kiểm tra lại");
                    flag = false;
                }
                else
                {
                    if (this.txtDepartmentName.Text == "")
                    {
                        this.errorProvider.SetError(this.txtDepartmentName, "Bạn chưa nhập tên đối tượng. Xin vui lòng kiểm tra lại");
                        flag = false;
                    }
                    else
                    {
                        flag = true;
                    }
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                flag = false;
            }
            return flag;
        }

        protected override void Edit()
        {
            ClsDepartmentBS obj = new ClsDepartmentBS();
            try
            {
                using (DsDepartment ds = new DsDepartment())
                {
                    DataRow drow = ds.DtBusinessObject.NewRow();
                    ds.DtBusinessObject.Rows.Add(drow);
                    drow["ID"] = this.ID;
                    drow["Code"] = this.txtDepartmentCode.Text.Trim();
                    drow["Name"] = this.txtDepartmentName.Text.Trim();
                    drow["Description"] = this.txtDescription.Text.Trim();
                    if (this.chkActive.Checked == true)
                    {
                        drow["Inactive"] = 1;
                    }
                    else
                    {
                        drow["Inactive"] = 0;
                    }
                    obj.DoEdit(ds.DtBusinessObject);
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }
    }
}